package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list",name = "查询所有企业信息")
    public String findAll(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Company> pageInfo = companyService.findByPage(page, pageSize);
        request.setAttribute("page", pageInfo);
        return "company/company-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到企业新增页面")
    public String add() {
        return "company/company-add";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到企业修改页面")
    public String toUpdate(String id) {
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        return "company/company-update";
    }

    @RequestMapping(value = "edit",name = "保存企业信息")
    public String save(Company company) {
        if (StringUtils.isEmpty(company.getId())) {
            String id = UUID.randomUUID().toString();
            company.setId(id);
            companyService.save(company);
        } else {
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "delete",name = "删除企业信息")
    public String deleteById(String id) {
        companyService.deleteById(id);
        return "redirect:/company/list.do";
    }
}
